import _ from 'lodash'
import { AVATAR } from '@/assets/js/settings'
export const state = () => {
  return {
    hasUser: false,
    id: -1,
    follow: 0,
    fans: 0,
    sex: '男',
    email: '1057966749@qq.com',
    name: '信息青年',
    avatar: '2019/11/25/3c018520-0f59-11ea-9a32-e1c0e5d78e95.jpg',
    dynamic: []
  }
}
export const getters = {
  avatar: (state) => {
    return `${AVATAR}/${state.avatar}`
  }
}
export const mutations = {
  setUser(state, data) {
    if (!_.isEmpty(data)) {
      state.hasUser = true
      for (const item in data) {
        state[item] = data[item]
      }
    }
  },
  setAvatar(state, data) {
    state.avatar = data
  },
  setName(state, data) {
    state.name = data
  },
  setSex(state, data) {
    state.sex = data
  },
  setAttention(state, data) {
    if (data === 1) {
      state.follow++
    } else if (data === -1) {
      state.follow--
    }
  },
  setDynamic(state, data) {
    state.dynamic = data
  }
}

export const actions = {
  setUser: ({ commit }, body) => {
    commit('setUser', body)
  },
  setName: async ({ commit }, body) => {
    const { status, data } = await body.axios.patch('/user', {
      name: body.data
    })
    if (status === 200 && data.code === 0) {
      commit('setName', body.data)
    }
  },
  setAvatar: async ({ commit }, body) => {
    const { status, data } = await body.axios.patch('/user', {
      avatar: body.data
    })
    if (status === 200 && data.code === 0) {
      commit('setAvatar', body.data)
    }
  },
  setSex: async ({ commit }, body) => {
    const { status, data } = await body.axios.patch('/user', {
      sex: body.data
    })
    if (status === 200 && data.code === 0) {
      commit('setSex', body.data)
    }
  },
  setDynamic: async ({ commit, state }, axios) => {
    const { status, data } = await axios.get('/user/dynamic', {
      params: {
        id: state.id,
        page: 1,
        limit: 5
      }
    })
    if (status === 200 && data.code === 1) {
      commit('setDynamic', data.data)
    }
  }
}
